package com.hongyuan.sellfitness.ui.person.view_mode;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.bus.RxBus;
import com.hongyuan.mvvmhabitx.bus.RxSubscriptions;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.base.ItemViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.ui.main.beans.ScanBeans;
import com.hongyuan.sellfitness.ui.person.beans.MyGroupBean;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class MyGroupViewModel extends CustomViewModel<MyRepository> {

    private Disposable mSubscription;

    public MyGroupViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<MyGroupItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<MyGroupItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_my_group);

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //扫码签到
        public SingleLiveEvent<Void> goScan = new SingleLiveEvent<>();
        //签到成功
        public SingleLiveEvent<Void> showSuccess = new SingleLiveEvent<>();
        //取消预约
        public SingleLiveEvent<Void> cancel = new SingleLiveEvent<>();
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mSubscription = RxBus.getDefault().toObservable(ScanBeans.class)
                .observeOn(AndroidSchedulers.mainThread()) //回调到主线程更新UI
                .subscribe(new Consumer<ScanBeans>() {
                    @Override
                    public void accept(ScanBeans scanBeans) throws Exception {

                        if(itemViewModel != null){
                            //扫码签到
                            setParams("ocs_id",String.valueOf(itemViewModel.entity.get().getOcs_id()))
                                    .setParams("type","qd");

                            requestData(Constants.SUPER_COURSE_XY_QD);
                        }
                    }
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        //取消订阅，防止内存泄漏
        RxSubscriptions.remove(mSubscription);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_MEMBER_SIGN_UP_COURSE_SUPER_LIST){
            List<MyGroupBean.ListBean> mList = ((MyGroupBean)dataBean).getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                observableList.clear();
            }

            if(mList != null && mList.size() > 0){
                for(MyGroupBean.ListBean bean : mList){
                    MyGroupItemViewModel itemViewModel = new MyGroupItemViewModel(this,bean);
                    observableList.add(itemViewModel);
                }
            }

            if(observableList.size() > 0){
                if(mList == null || mList.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }else{

            }
        }

        if(code == Constants.SUPER_COURSE_XY_QD){

            if(itemViewModel != null){
                itemViewModel.entity.get().setXy_qd_state(1);
                itemViewModel.entity.notifyChange();

                uc.showSuccess.call();
            }

        }

        if(code == Constants.CANCEL_COURSE_SUPER_ORDER){
            if(itemViewModel != null){
                observableList.remove(itemViewModel);
            }
        }
    }

    //操作的子项
    public MyGroupItemViewModel itemViewModel;

    @Override
    public void itemClick(ItemViewModel itemVM) {
        super.itemClick(itemVM);

        itemViewModel = (MyGroupItemViewModel) itemVM;

    }

}
